package com.example.wechat_b.activity;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.wechat_b.R;
import com.example.wechat_b.activity.base.BaseActivity;
import com.example.wechat_b.adapter.ViewPagerFragmentAdapter;
import com.example.wechat_b.fragment.ChatFragment;
import com.example.wechat_b.fragment.AdapterFragment;
import com.example.wechat_b.fragment.FindFragment;
import com.example.wechat_b.fragment.FriendFragment;
import com.example.wechat_b.fragment.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener{

    private ViewPager vp;
    private TextView Title_text;
    private BottomNavigationView bottomNavigationView;
    private List<Fragment> fragments;
    private ViewPagerFragmentAdapter viewPagerFragmentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //注意要清除 FLAG_TRANSLUCENT_STATUS flag
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.min_gray));
        MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.min_gray));
        setContentView(R.layout.activity_main);
        ActionBar bar =  getSupportActionBar();
        if (bar.isShowing()){
            bar.hide();
        }

        initView();
        initEvent();
    }

    private void initView() {
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        vp = findViewById(R.id.main_viewpager);
        fragments = new ArrayList<>();
        fragments.add(new ChatFragment());
        fragments.add(new FriendFragment());
        fragments.add(new FindFragment());
        fragments.add(new MineFragment());
        viewPagerFragmentAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(),fragments);
        vp.setAdapter(viewPagerFragmentAdapter);
    }

    private void initEvent() {
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.bottom_message:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.bottom_friend:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.bottom_compass:
                        vp.setCurrentItem(2);
                        break;
                    case R.id.bottom_mine:
                        vp.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        bottomNavigationView.setSelectedItemId(R.id.bottom_message);
                        break;
                    case 1:
                        bottomNavigationView.setSelectedItemId(R.id.bottom_friend);
                        break;
                    case 2:
                        bottomNavigationView.setSelectedItemId(R.id.bottom_compass);
                        break;
                    case 3:
                        bottomNavigationView.setSelectedItemId(R.id.bottom_mine);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bottom_ic_message:
                break;
        }
    }
}